// 模块化
var M1 = function () {
  var m1 = "from m1";
  return {
    m1,
  };
};
var M2 = function () {
  var m1 = M1().m1;
  function logMs() {
    console.log(`hello, I am ${m1}`);
  }
  return {
    logMs,
  };
};
const log = M2().logMs;
log();

// 惰性函数
function getStyle(element, attr) {
  if ("getComputedStyle" in window) {
    getStyle = function (element, attr) {
      return window.getComputedStyle(element)[attr];
    };
  } else {
    getStyle = function (element, attr) {
      return element.currentStyle[attr];
    };
  }
  return getStyle(element, attr);
}
const box = document.getElementById("box");
console.log(getStyle(box, "height"));

// 柯里化：预先处理的思想。利用闭包的特性，把一些变量存储起来并预先进行处理；以后基于作用域链，就可以跳过预先处理的环节，直接进行后续处理
function curring(x) {
  return function (...args) {
    args.unshift(x);
    return args.reduce(function (pre, next) {
      return pre + next;
    });
  };
}
const initial = curring(10);
console.log(initial(20));
console.log(initial(30, 40));

// 组合函数
function compose(...fns) {
  if (fns.length === 0) {
    return (arg) => arg;
  }
  if (fns.length === 1) {
    return fns[0];
  }
  return fns.reduce(
    (pre, next) =>
      (...args) =>
        next(pre(...args))
  );
}
function f1(n) {
    return n + 1;
  }
  function f2(n) {
    return n + 2;
  }
  console.log(compose(f1, f2)(10))